//
//  API_UIBezierPath_init_DemoVC.swift
//  APSwift
//
//  Created by jianbo.pei on 2022/4/16.
//

import UIKit
import SnapKit

let sw = UIScreen.main.bounds.size.width

class API_UIBezierPath_init_DemoVC: UIViewController {

    private lazy var vStackView: UIStackView = {
        let stackV = UIStackView()
        stackV.axis = .vertical
        stackV.distribution = .equalSpacing
        stackV.spacing = 30
        return stackV
    }()
    
    private lazy var scrollView: UIScrollView = {
        let scrollV = UIScrollView()
        scrollV.showsVerticalScrollIndicator = false
        scrollV.showsHorizontalScrollIndicator = false
        return scrollV
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        view.addSubview(scrollView)
        scrollView.backgroundColor = .lightGray
        scrollView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        scrollView.addSubview(vStackView)
        vStackView.backgroundColor = .red
        vStackView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        /// 椭圆
        let ovalV = API_Oval_View()
        vStackView.addArrangedSubview(ovalV)
        ovalV.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: sw, height: 280))
        }
        
        /// 圆形
        let circleV = API_Circle_View()
        vStackView.addArrangedSubview(circleV)
        circleV.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: sw, height: 350))
        }
        
        /// 圆角矩形：指定矩形的左下角和右上角为圆角
        let rectangleV1 = API_Rectangle1_View()
        vStackView.addArrangedSubview(rectangleV1)
        rectangleV1.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: sw, height: 300))
        }
    }


}


//!!!: 椭圆
class API_Oval_View: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .purple
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func draw(_ rect: CGRect) {
        super.draw(rect)
        
        let color: UIColor = .red
        color.set()
        
        /// 当长和宽相等时则为圆；当长和宽不等时为椭圆
        let bezierPath = UIBezierPath.init(ovalIn: CGRect(x: 100, y: 100, width: 150, height: 80))
        bezierPath.lineWidth = 3
        bezierPath.stroke()
        
    }
}

//!!!: 圆形
class API_Circle_View: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .purple
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func draw(_ rect: CGRect) {
        super.draw(rect)
        
        let color: UIColor = .yellow
        color.set()
        
        let bezierPath = UIBezierPath.init(ovalIn: CGRect(x: 100, y: 100, width: 150, height: 150))
        bezierPath.lineWidth = 3
        bezierPath.stroke()
        
    }
}

//!!!: 圆角矩形：指定矩形的左下角和右上角为圆角
class API_Rectangle1_View: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .purple
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func draw(_ rect: CGRect) {
        super.draw(rect)
        
        let color = UIColor.cyan
        color.setStroke()
        
        let bezierPath = UIBezierPath.init(roundedRect: CGRect(x: 100, y: 100, width: 150, height: 100), byRoundingCorners: UIRectCorner(rawValue: (UIRectCorner.topRight.rawValue | UIRectCorner.bottomLeft.rawValue)), cornerRadii: CGSize(width: 20, height: 20))
        bezierPath.lineWidth = 3
        bezierPath.stroke()
        
    }
}

